package cn.flying.cloud.oauth.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.flying.cloud.oauth.entity.OauthUserDetail;
import cn.flying.cloud.oauth.mapper.OauthUserDetailMapper;
import cn.flying.cloud.oauth.service.OauthUserDetailService;

/**
 * 用户信息服务类
 *
 * @author: admin
 * @date: 2023年04月11日 17:48
 * @version: 1.0
 */
@Service
public class OauthUserDetailServiceImpl extends ServiceImpl<OauthUserDetailMapper, OauthUserDetail> implements OauthUserDetailService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private PasswordEncoder passwordEncoder;

    /**
     * 查询用户认证信息
     *
     * @param username
     * @param type
     * @return
     */
    @Override
    public OauthUserDetail getOauthUserDetail(String username, String type) {
        LambdaQueryWrapper<OauthUserDetail> wrapper = new LambdaQueryWrapper<>();
        if ("mobile".equals(type)) {
            wrapper.eq(OauthUserDetail::getMobile, username);
        } else if ("email".equals(type)) {
            wrapper.eq(OauthUserDetail::getEmail, username);
        } else {
            // 默认按照用户名查询
            wrapper.eq(OauthUserDetail::getUsername, username);
        }
        OauthUserDetail userDetail = this.getOne(wrapper);
        return userDetail;
    }
}
